<template>
	<view style="width:100%;height:300upx;display: flex;align-items: center;justify-content: center;">
		<view v-if="errMsg.length>0" style="font-size: 36upx;">{{errMsg}}</view>
	</view>
</template>

<script>
	var page;
	var params;
	export default {
		data() {
			return {
				errMsg: "",
			}
		},
		onLoad: function(options) {
			console.log("index------------------>",options)
			var that = this;
			// if (!options.appid || options.appid == "null" || options.appid == "") {
			// 	this.errMsg = "参数错误，未设置appid！";
			// 	return
			// } else if ((!options.secret || options.appid == "null" || options.appid == "") && (!options.openid || options.openid ==
			// 		"null" || options.openid == "") && (!options.openId || options.openId == "null" || options.openId == "")) {
			// 	this.errMsg = "参数错误，未设置secret或openid！";
			// 	return
			// } 

			// console.log("url参数------------------->", options.param);
			// if (options.param) {
			// 	params = JSON.parse(decodeURIComponent(options.param));
			// }

			page = options.page || "0";
			// let secret = options.secret || "";
			let appid = this.pub.appId
			// let origin = options.origin;

			uni.removeStorageSync("secret");
			uni.removeStorageSync("appid");
			uni.removeStorageSync("openid");
			uni.removeStorageSync("account");
			uni.removeStorageSync("userinfo");
			uni.removeStorageSync("headimage");
			uni.removeStorageSync("user");
			// uni.removeStorageSync("origin"); //来源，万维微游甘肃为"wygs"

			// if (origin && origin != "null" && origin != "") {
			// 	this.pub.setOrigin(origin)
			// }

			this.pub.setAppid(appid)

			//如果存在userinfo,则不用再去自己获取userinfo
			if (options.userinfo && options.userinfo != "null" && options.userinfo != "") {
				this.userinfo = JSON.parse(decodeURIComponent(options.userinfo));
				this.pub.setUserInfo(options.userinfo);
				this.pub.getEncryptionOid(this.userinfo, function(res) {
					if (!res) {
						console.log("获取用户信息失败!")
					} else {
						console.log("获取用户信息成功!")
					}
					//跳转具体界面
					that.dispatch();
				})
			} else if (appid && appid != "null" && appid != "") {
				// this.pub.setSecret(secret)
				//#ifdef H5
				//获取openid

				if (this.isWeiXin()) {
					this.getOpenidByJssDk(function(res) {
						if (res) {
							//通过上secret传进来的，说明是自己的公众
							that.pub.setAccount(that.pub.getOpenid());
							that.pub.setHeadImage(
								"https://wx.qlogo.cn/mmopen/vi_32/B9xu1Nq83qYQ60qj2FTv0cyJdQv2hwmdsg0cFO8CjHUzSD44kn9UZp2gico2ibNCByN9lZhbmuRpIjVbkm4TUJFg/132"
							)
							// that.pub.encryptUserInfo("",that.pub.getOpenid(),"",that.pub.getHeadImage(),that.pub.getOpenid(),that.pub.getOpenid(),function(res){
							// 	console.log(res)
							// 	that.pub.setUserInfo(res)
							that.dispatch()
							// })
						}
					})
				} else {
					this.errMsg = "暂时不支持非公众号访问"
				}
				//#endif
			}
		},
		methods: {
			getOpenidByJssDk(callback) {
				callback = callback || function(res) {

				}
				// #ifdef H5
				let appid = this.pub.getAppid();
				// let secret = this.pub.getSecret();
				if (!appid) {
					callback(false)
					return
				}
				let originUrl = window.location.href;
				let code = "";
				if (window.location.href.indexOf("code") != -1) {
					code = this.pub.getQueryString("code")
				}
				if (code == null || code == undefined || code == "") {
					let link =
						`https://open.weixin.qq.com/connect/oauth2/authorize?
					appid=${appid}&redirect_uri=${encodeURIComponent(originUrl)}&reurl=11&response_type=code&scope=${'snsapi_userinfo'}&state=321#wechat_redirect`
					window.location.href = link
				} else {
					let params = {
						code: code, //要去换取openid的登录凭证
						channelType: this.pub.channelType
					}
					// let _that = this
					this.$postApi(this.$path.WX_GET_OPENIDH5, params).then(res => {
						console.log("获取的用户信息", res.data)
						if (res.data.success) {
							this.pub.setUserInfo(res.data.data)
							this.getIntegral(res.data.data.memberId)
						} else {
							this.pub.showError(res.data.error.errorMessage)
						}
						// _that.pub.setOpenid(res.data.openid)
						// this.pub.setUserInfo(res.data)
						callback(true)
					}).fail(data => {
						callback(false)
					})
				}
				// #endif
			},
			getIntegral: function(memberId) {
				let params = {
					channelType: this.pub.channelType,
					memberId: memberId
				}
				this.$postApi(this.$path.GET_INTERGRAL_BY_ID, params).then(res => {
					console.log("登录用户积分数据返回-------->", res.data)
					if (res.data.success) {
						let userinfo = this.pub.getUserInfo()
						userinfo.integral = res.data.data.integral
						this.pub.setUserInfo(userinfo)
					}
				})
			},
			isWeiXin() {
				// #ifdef H5
				var ua = window.navigator.userAgent.toLowerCase();
				if (ua.match(/MicroMessenger/i) == 'micromessenger') {
					return true;
				} else {
					return false;
				}
				//#endif
				return false;
			},
			dispatch: function() {
				switch (page) {
					case "0": //首页
						uni.redirectTo({
							url: "/pages/home_new/home_new"
						})
						break;
					case "1": //我的

						uni.redirectTo({
							url: "/pages/my/minepage_new"
						})

						break;
					case "2": //积分商城
						uni.redirectTo({
							url: "/pages/integral/integral"
						})
						break;
					case "3": //资讯列表
						uni.redirectTo({
							url: "/pages/message/message"
						})
						break;
					case "4": //酒店详情
						{
							let inDate = null;
							let outDate = null;
							let hotelid = null;
							let trueInDate = null;
							let trueOutDate = null;
							if (params) {
								// inDate = params.inDate;
								// outDate = params.outDate;
								hotelid = params.hotelid;
								trueInDate = params.trueInDate;
								trueOutDate = params.trueOutDate;
							}
							if (!hotelid || hotelid == "") {
								this.errMsg = "参数错误，未传hotelid!";
								return
							}
							uni.redirectTo({
								url: "/pages/hotel/hoteldetail?hotelid=" + hotelid + "&trueInDate=" + trueInDate + "&trueOutDate=" +
									trueOutDate,
							})
						}
						break;
					case "5": //购物
						uni.redirectTo({
							url: "/pages/shopping/shoppinghome"
						})
						break;
					case "6": //购物详情
						uni.redirectTo({
							url: "/pages/shopping/shoppingdetail?item=" + encodeURIComponent(JSON(param))
						})
						break;
					case "7": //购物下单确认，提供给购物时，跳转登录页面，登录成功后返回
						uni.redirectTo({
							url: "/pages/shopping/shoppingconfirm?item=" + encodeURIComponent(JSON(param))
						})
						break;
					case "20": //订单列表
						{
							let type = null;
							if (params) {
								type = params.type;
							}
							uni.redirectTo({
								url: "/pages/order/neworderlist?item=" + type
							})
						}
						break;
					default:
						this.errMsg = "该页面不存在，请检查page参数!"
						break;
				}
			},
		}
	}
</script>

<style>

</style>
